﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Threading;

namespace ajayumi.Platform.Win.UserCtlPanel
{
    /// <summary>
    /// UserCtlPanelListBase.cs
    /// Author   : 黄乙冬
    /// Date     : 2011-09-25
    /// </summary>
    [ToolboxItem(false)]
    public partial class UserCtlPanelListBase : UserCtlPanelBase, IPagination
    {
        #region Variables

        private bool m_IsPaginationable = true;
        private string m_QueryWhere = "1 = 1";
        private int m_PageIndex = 1;
        private int m_PageCount = 1;
        private int m_PageSize = 10;
        private int m_TotalRecoder = 0;

        private Control m_PageController = null;

        private Button m_FirstPageButton = null;
        private Button m_PrevPageButton = null;
        private Button m_NextPageButton = null;
        private Button m_LastPageButton = null;
        private Label m_PageCountLabel = null;

        #endregion


        public UserCtlPanelListBase()
        {
            InitializeComponent();

            if (!DesignMode)
            { this.Init(); }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        protected virtual void Init()
        { }

        protected override void ShowBase()
        {
            if (this.IsPaginationable)
            {
                this.m_PageController = this.InstancePageController();
                this.m_PageController.Dock = DockStyle.Bottom;
                this.m_PageController.BringToFront();
                this.Controls.Add(this.m_PageController);
                this.RegisterPageCommand();
            }
        }

        /// <summary>
        /// 实例化分页控件
        /// </summary>
        protected virtual Control InstancePageController()
        {
            return new Pager();
        }

        /// <summary>
        /// 注册分页命令
        /// </summary>
        protected virtual void RegisterPageCommand()
        {
            if (this.FirstPageButton != null)
            {
                this.FirstPageButton.Click +=
                  (sender, e) => { this.FirstPage(); };
            }
            if (this.PrevPageButton != null)
            {
                this.PrevPageButton.Click +=
                  (sender, e) => { this.PrevPage(); };
            }
            if (this.NextPageButton != null)
            {
                this.NextPageButton.Click +=
                  (sender, e) => { this.NextPage(); };
            }
            if (this.LastPageButton != null)
            {
                this.LastPageButton.Click +=
                  (sender, e) => { this.LastPage(); };
            }
        }

        /// <summary>
        /// 分页按钮状态
        /// </summary>
        protected virtual void PageButtonStatus()
        {
            if (!this.IsPaginationable) return;

            this.DisplayPageInfo();

            if (this.PageIndex == 1)
            {
                this.FirstPageButton.Enabled = false;
                this.PrevPageButton.Enabled = false;
            }
            else
            {
                this.FirstPageButton.Enabled = true;
                this.PrevPageButton.Enabled = true;
            }


            if (this.PageIndex >= this.PageCount)
            {
                this.LastPageButton.Enabled = false;
                this.NextPageButton.Enabled = false;
            }
            else
            {
                this.LastPageButton.Enabled = true;
                this.NextPageButton.Enabled = true;
            }
        }

        /// <summary>
        /// 显示分页的信息
        /// </summary>
        protected virtual void DisplayPageInfo()
        {
            this.PageInfoLabel.Text = string.Format("页码：{0}/{1}，总记录数：{2}", this.PageIndex, this.PageCount, this.TotalRecoder);
        }


        #region IPagination

        #region 分页属性信息

        /// <summary>
        /// 是否启用分页
        /// </summary>
        public bool IsPaginationable
        {
            get
            {
                return this.m_IsPaginationable;
            }
            set
            {
                this.m_IsPaginationable = value;
                if (null != this.m_PageController)
                {
                    this.m_PageController.Visible = value;
                }                
            }
        }

        /// <summary>
        /// 当前查询条件
        /// </summary>
        public string QueryWhere
        {
            get
            {
                if (string.IsNullOrEmpty(this.m_QueryWhere))
                { this.m_QueryWhere = "1 = 1"; }
                return this.m_QueryWhere;
            }
            set
            {
                this.m_QueryWhere = value;
            }
        }
        /// <summary>
        /// 当前分页页面索引
        /// </summary>
        public int PageIndex
        {
            get
            { return this.m_PageIndex; }
            set
            { this.m_PageIndex = value; }
        }
        /// <summary>
        /// 当前分页页面数量
        /// </summary>
        public int PageCount
        {
            get
            { return this.m_PageCount; }
            set
            { this.m_PageCount = value; }
        }
        /// <summary>
        /// 每页显示的记录数
        /// </summary>
        public virtual int PageSize
        {
            get
            { return this.m_PageSize; }
            set
            { this.m_PageSize = value; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecoder
        {
            get
            { return this.m_TotalRecoder; }
            set
            { this.m_TotalRecoder = value; }
        }

        #endregion


        public virtual string FirstPageConID
        {
            get { return "FirstPage"; }
        }

        public virtual string PrevPageConID
        {
            get { return "PrevPage"; }
        }

        public virtual string NextPageConID
        {
            get { return "NextPage"; }
        }

        public virtual string LastPageConID
        {
            get { return "LastPage"; }
        }

        #region Unused
        //public virtual string GoToPageConID
        //{
        //    get { return "GoToPage"; }
        //}

        //public virtual string InputPageConID
        //{
        //    get { return "InputPage"; }
        //}

        //public virtual string DigitPageConID
        //{
        //    get { return "DigitalPage"; }
        //}

        //public virtual string PageSizeConID
        //{
        //    get { return "PageSize"; }
        //}
        #endregion

        public string PageDetailConId
        {
            get { return "PageInfo"; }
        }

        public string DigitPageConID
        {
            get { throw new NotImplementedException(); }
        }

        public void DirectPage(int pageIndex)
        {
            throw new NotImplementedException();
        }

        public void FirstPage()
        {
            this.PageIndex = 1;
            this.RunWorkAsync();
        }

        public void PrevPage()
        {
            if (this.PageIndex != 1)
            {
                this.PageIndex = Convert.ToInt32(this.PageIndex) - 1;
            }
            this.RunWorkAsync();
        }

        public void NextPage()
        {
            if (this.PageIndex != this.PageCount)
            {
                this.PageIndex = Convert.ToInt32(this.PageIndex) + 1;
            }
            this.RunWorkAsync();
        }

        public void LastPage()
        {
            this.PageIndex = this.PageCount;
            this.RunWorkAsync();
        }

        public void GoToPage(int pageIndex)
        {
            if (pageIndex > this.PageCount || pageIndex <= 0)
            {
                return;
            }

            this.PageIndex = pageIndex;
            this.RunWorkAsync();
        }

        #endregion

        #region PageController

        protected Button FirstPageButton
        {
            get
            {
                if (this.m_PrevPageButton == null)
                {
                    this.m_PrevPageButton = this.FindControlInPager<Button>(this.FirstPageConID);
                }
                return m_PrevPageButton;
            }
        }

        protected Button PrevPageButton
        {
            get
            {
                if (this.m_FirstPageButton == null)
                {
                    this.m_FirstPageButton = this.FindControlInPager<Button>(this.PrevPageConID);
                }
                return m_FirstPageButton;
            }
        }

        protected Button NextPageButton
        {
            get
            {
                if (this.m_NextPageButton == null)
                {
                    this.m_NextPageButton = this.FindControlInPager<Button>(this.NextPageConID);
                }
                return m_NextPageButton;
            }
        }


        protected Button LastPageButton
        {
            get
            {
                if (this.m_LastPageButton == null)
                {
                    this.m_LastPageButton = this.FindControlInPager<Button>(this.LastPageConID);
                }
                return m_LastPageButton;
            }
        }


        private Label PageInfoLabel
        {
            get
            {
                if (this.m_PageCountLabel == null)
                {
                    this.m_PageCountLabel = this.FindControlInPager<Label>(this.PageDetailConId);
                }
                return m_PageCountLabel;
            }
        }

        /// <summary>
        /// 查找分页控件中的指定控件
        /// </summary>
        /// <typeparam name="T">控件泛型</typeparam>
        /// <param name="key">控件名称</param>
        /// <returns></returns>
        protected T FindControlInPager<T>(string key) where T : Control
        {
            T control = default(T);
            if (!this.IsPaginationable) return control;

            Control[] controls = this.m_PageController.Controls.Find(key, true);
            foreach (Control item in controls)
            {
                if (item is T)
                {
                    control = item as T;
                    break;
                }
            }
            return control;
        }

        #endregion


    }
}
